package com.oriental.manage.controller.report;

import com.oriental.check.commons.util.BeanMapperUtil;
import com.oriental.check.commons.util.DateUtil;
import com.oriental.manage.core.excelUtils.ExcelTemplateUtil;
import com.oriental.manage.core.exception.BusiException;
import com.oriental.manage.core.fileUtils.ExcelContentExt;
import com.oriental.manage.core.fileUtils.FileDownAjax;
import com.oriental.manage.core.fileUtils.HeaderExt;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.core.system.log.OperateLogger;
import com.oriental.manage.core.utils.Bean2MapUtil;
import com.oriental.manage.core.utils.DateUtils;
import com.oriental.manage.core.utils.SessionUtils;
import com.oriental.manage.core.utils.StringCommonUtils;
import com.oriental.manage.pojo.report.DayAccountReportData;
import com.oriental.manage.pojo.report.DayBalanceReportData;
import com.oriental.paycenter.commons.mode.Response;
import com.oriental.settlementfront.service.facade.manager.DayBalanceReportFacade;
import com.oriental.settlementfront.service.facade.manager.model.DayBalanceReportBankDTO;
import com.oriental.settlementfront.service.facade.manager.model.DayBalanceReportDTO;
import com.oriental.settlementfront.service.facade.manager.model.DayBalanceReportMchntDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.webbuilder.office.excel.config.ExcelWriterConfig;
import org.webbuilder.office.excel.config.Header;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;

/**
 * <ul>
 * <li>支付机构,商户侧日结平衡表</li>
 * <li>User:蒯越 Date:2017/2/22 Time:15:15</li>
 * </ul>
 */
@Slf4j
@Controller
public class DayBalanceReportController {

    @Autowired
    private DayBalanceReportFacade dayBalanceReportFacade;
    @Autowired
    private FileDownAjax fileDownAjax;
    @Autowired
    private com.oriental.manage.core.system.Constants Constants;

    @RequestMapping("/report/dayBalanceReport/initBusi")
    public String initBusi() {
        return "report/searchBusiDayBalanceReport";
    }

    @RequestMapping("/report/dayBalanceReport/initFund")
    public String initFund() {
        return "report/searchFundDayBalanceReport";
    }

    @OperateLogger(content = "查询支付机构,商户侧日结平衡表", operationType = OperateLogger.OperationType.R)
    @RequestMapping("/report/dayBalanceReport/search")
    @RequiresPermissions("dayBalanceReport_search")
    @ResponseBody
    public ResponseDTO<List<DayAccountReportData>> search(String sDate) {
        ResponseDTO<List<DayAccountReportData>> responseDTO = new ResponseDTO<>();
        log.info("查询信息:{}", sDate);
        try {
            Response<DayBalanceReportDTO> response = dayBalanceReportFacade.selectDayBalanceReport(
                    DateUtil.parse(sDate), SessionUtils.getUserName());
            log.info("查询日结平衡表数据,查询结果【{}】", response);
            if (!response.isSuccess()) {
                throw new BusiException(response.getErrorCode(), response.getErrorMsg());
            }
            DayBalanceReportDTO dayBalanceReportDTO = response.getResult();
            List<DayBalanceReportBankDTO> dayBalanceReportBankDTOList
                    = dayBalanceReportDTO.getDayBalanceReportBankDTOList();
            List<DayBalanceReportMchntDTO> dayBalanceReportMchntDTOList
                    = dayBalanceReportDTO.getDayBalanceReportMchntDTOList();
            DayBalanceReportData sumData = new DayBalanceReportData();
            //组装返还对象保证相同银行银行名称，金额，差异一致
            Map<String,DayAccountReportData> dayAccountReportDataMap = new HashMap<>();
            for(DayBalanceReportBankDTO dayBalanceReportBankDTO : dayBalanceReportBankDTOList){
                DayBalanceReportData data;
                if(dayBalanceReportBankDTO.getPayOrgCode() != null){
                    if(dayBalanceReportMchntDTOList.size() > 0){
                        data = BeanMapperUtil.objConvert(
                                dayBalanceReportBankDTO, DayBalanceReportData.class);
                        this.fillBankTransData(dayBalanceReportBankDTO, data);
                        BeanMapperUtil.copy(dayBalanceReportMchntDTOList.get(0), data);
                        this.fillMchntTransData(dayBalanceReportMchntDTOList.get(0), data);
                        dayBalanceReportMchntDTOList.remove(0);
                    }else { //商户侧集合没有数据时，默认赋值
                        data = BeanMapperUtil.objConvert(
                                dayBalanceReportBankDTO, DayBalanceReportData.class);
                        this.fillBankTransData(dayBalanceReportBankDTO, data);
                    }
                }else {
                    data = BeanMapperUtil.objConvert(
                            dayBalanceReportBankDTO, DayBalanceReportData.class);
                    this.fillBankTransData(dayBalanceReportBankDTO, data);
                }

                DayAccountReportData dayAccountReportData = dayAccountReportDataMap.get(data.getProvisionsAccountName());
                if(dayAccountReportData == null){
                    dayAccountReportData = new DayAccountReportData();
                    dayAccountReportDataMap.put(data.getProvisionsAccountName(),dayAccountReportData);
                }
                dayAccountReportData.setDifferenceAmt(data.getBankTransAmtD() - data.getBankTransAmtC() + dayAccountReportData.getDifferenceAmt() - data.getProvisionsAccrualAmt());
                dayAccountReportData.setProvisionsAccountName(data.getProvisionsAccountName());
                dayAccountReportData.setProvisionsAccrualAmt(data.getProvisionsAccrualAmt() + dayAccountReportData.getProvisionsAccrualAmt());
                dayAccountReportData.setReceiveEarlierAmt(data.getReceiveEarlierAmt() + dayAccountReportData.getReceiveEarlierAmt());
                dayAccountReportData.setNonBusinessAmt(data.getNonBusinessAmt() + dayAccountReportData.getNonBusinessAmt());
                //支付机构号为空页面不显示
                if(!(data.getPayOrgCode() == null && dayAccountReportData.getDayBalanceReportDataList().size() > 0)){
                    dayAccountReportData.getDayBalanceReportDataList().add(data);
                }
                //统计每列合计值
                this.fillSumData(sumData, data);
                //判断数据是否平衡
                this.fillDetailDataIsRed(data);

            }
            //商户侧集合数据还有剩余，默认赋值
            for(DayBalanceReportMchntDTO dayBalanceReportMchntDTO : dayBalanceReportMchntDTOList){
                DayBalanceReportData data = BeanMapperUtil.objConvert(
                        dayBalanceReportMchntDTO, DayBalanceReportData.class);
                this.fillMchntTransData(dayBalanceReportMchntDTO, data);
                DayAccountReportData dayAccountReportData = dayAccountReportDataMap.get(null);
                if(dayAccountReportData == null){
                    dayAccountReportData = new DayAccountReportData();
                }
                dayAccountReportData.getDayBalanceReportDataList().add(data);
                //统计每列合计值
                this.fillSumData(sumData, data);
                //判断数据是否平衡
                this.fillDetailDataIsRed(data);
            }
            List<DayAccountReportData> dayAccountReportDataList = new ArrayList<>();
            List<DayAccountReportData> dayAccountReportDataListNullPayOrMchnt = new ArrayList<>();
            List<DayAccountReportData> dayAccountReportDataListNullPay = new ArrayList<>();
            List<DayAccountReportData> dayAccountReportDataListNullAcc = new ArrayList<>();
            List<DayAccountReportData> dayAccountReportDataListNullMchnt = new ArrayList<>();
            for(String key : dayAccountReportDataMap.keySet()){
                //判断备付金数据是否平衡
                this.fillDayAccountIsRed(dayAccountReportDataMap.get(key));
                int i = dayAccountReportDataMap.get(key).getDayBalanceReportDataList().size() -1;
                if(dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(i) != null && dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(i).getPayOrgCode() != null
                        && dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(i).getMchntCode() != null) {
                    dayAccountReportDataList.add(dayAccountReportDataMap.get(key));
                }else if(dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(0) != null && dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(0).getPayOrgCode() != null
                        && dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(0).getMchntCode() != null) {
                    dayAccountReportDataListNullPayOrMchnt.add(dayAccountReportDataMap.get(key));
                }else if(dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(0) != null && dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(0).getPayOrgCode() != null
                        && dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(0).getProvisionsAccountName() != null){
                    dayAccountReportDataListNullAcc.add(dayAccountReportDataMap.get(key));
                }else if(dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(0) != null && dayAccountReportDataMap.get(key).getDayBalanceReportDataList().get(0).getPayOrgCode() != null){
                    dayAccountReportDataListNullPay.add(dayAccountReportDataMap.get(key));
                }else {
                    dayAccountReportDataListNullMchnt.add(dayAccountReportDataMap.get(key));
                }
                this.fillSumDataDifferenceAmt(sumData, dayAccountReportDataMap.get(key));
            }
            dayAccountReportDataList.addAll(dayAccountReportDataListNullPayOrMchnt);
            dayAccountReportDataList.addAll(dayAccountReportDataListNullAcc);
            dayAccountReportDataList.addAll(dayAccountReportDataListNullPay);
            dayAccountReportDataList.addAll(dayAccountReportDataListNullMchnt);
            //判断合计数据是否平衡
            this.fillSumDataIsRed(sumData);
            responseDTO.setSuccess(true);
            responseDTO.setObject(dayAccountReportDataList);
            responseDTO.setSumObject(sumData);
        } catch (BusiException e) {
            log.error("查询失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        } catch (Exception e) {
            log.error("查询失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    private void fillDayAccountIsRed(DayAccountReportData dayAccountReportData){
        Long amt1 = this.getAmt(dayAccountReportData.getDifferenceAmt());
        Long amt2 = 0L;
        for(DayBalanceReportData dayBalanceReportData : dayAccountReportData.getDayBalanceReportDataList()){
            amt2 += this.getAmt(dayBalanceReportData.getNoReceiveAmt()) -
                    this.getAmt(dayBalanceReportData.getBeforeNoReceiveAmt()) + this.getAmt(dayBalanceReportData.getBeforeReceiveEarlierAmt());
        }
        amt2 = amt2 - dayAccountReportData.getReceiveEarlierAmt() - dayAccountReportData.getNonBusinessAmt();
        if (amt1.equals(amt2)) {
            dayAccountReportData.setRed(false);
        } else {
            dayAccountReportData.setRed(true);
        }
    }


    private void fillDetailDataIsRed(DayBalanceReportData data) {
        Long amt1 = this.getAmt(data.getCorrespondOrgAmtC()) - this.getAmt(data.getCorrespondOrgAmtD());
        Long amt2 = this.getAmt(data.getMchntTransAmtC())
                - this.getAmt(data.getDateNotMatchAmt()) + this.getAmt(data.getPayMoreAmt())
                - this.getAmt(data.getCorrespondOrgAmtD())
                - this.getAmt(data.getMchntLaterAmt()) + this.getAmt(data.getPayBeforeAmt());
        if (amt1.equals(amt2)) {
            data.setRed(false);
        } else {
            data.setRed(true);
        }
    }

    private void fillSumDataIsRed(DayBalanceReportData data) {
        Long amt1 = this.getAmt(data.getBankTransAmtD()) - this.getAmt(data.getBankTransAmtC());
        Long amt2 = this.getAmt(data.getCorrespondOrgAmtC()) - this.getAmt(data.getCorrespondOrgAmtD());
        Long amt3 = this.getAmt(data.getMchntTransAmtC())
                - this.getAmt(data.getDateNotMatchAmt()) + this.getAmt(data.getPayMoreAmt())
                - this.getAmt(data.getCorrespondOrgAmtD())
                - this.getAmt(data.getMchntLaterAmt()) + this.getAmt(data.getPayBeforeAmt());
        if (amt1.equals(amt2) && amt1.equals(amt3)) {
            data.setRed(false);
        } else {
            data.setRed(true);
        }
    }

    private void fillSumData(DayBalanceReportData sumData, DayBalanceReportData data) {
        sumData.setBankTransCountD(this.getCount(sumData.getBankTransCountD())
                + this.getCount(data.getBankTransCountD()));
        sumData.setBankTransAmtD(this.getAmt(sumData.getBankTransAmtD())
                + this.getAmt(data.getBankTransAmtD()));
        sumData.setBankTransCountC(this.getCount(sumData.getBankTransCountC())
                + this.getCount(data.getBankTransCountC()));
        sumData.setBankTransAmtC(this.getAmt(sumData.getBankTransAmtC())
                + this.getAmt(data.getBankTransAmtC()));
        sumData.setProvisionsAccrualAmt(this.getAmt(sumData.getProvisionsAccrualAmt())
                + this.getAmt(data.getProvisionsAccrualAmt()));
        sumData.setNoReceiveAmt(this.getAmt(sumData.getNoReceiveAmt())
                + this.getAmt(data.getNoReceiveAmt()));
        sumData.setReceiveEarlierAmt(this.getAmt(sumData.getReceiveEarlierAmt())
                + this.getAmt(data.getReceiveEarlierAmt()));
        sumData.setBeforeNoReceiveAmt(this.getAmt(sumData.getBeforeNoReceiveAmt())
                + this.getAmt(data.getBeforeNoReceiveAmt()));
        sumData.setBeforeReceiveEarlierAmt(this.getAmt(sumData.getBeforeReceiveEarlierAmt())
                + this.getAmt(data.getBeforeReceiveEarlierAmt()));
        sumData.setNonBusinessAmt(this.getAmt(sumData.getNonBusinessAmt())
                + this.getAmt(data.getNonBusinessAmt()));
        sumData.setCorrespondOrgCountC(this.getCount(sumData.getCorrespondOrgCountC())
                + this.getCount(data.getCorrespondOrgCountC()));
        sumData.setCorrespondOrgAmtC(this.getAmt(sumData.getCorrespondOrgAmtC())
                + this.getAmt(data.getCorrespondOrgAmtC()));
        sumData.setCorrespondOrgCountD(this.getCount(sumData.getCorrespondOrgCountD())
                + this.getCount(data.getCorrespondOrgCountD()));
        sumData.setCorrespondOrgAmtD(this.getAmt(sumData.getCorrespondOrgAmtD())
                + this.getAmt(data.getCorrespondOrgAmtD()));
        sumData.setMchntTransCountC(this.getCount(sumData.getMchntTransCountC())
                + this.getCount(data.getMchntTransCountC()));
        sumData.setMchntTransAmtC(this.getAmt(sumData.getMchntTransAmtC())
                + this.getAmt(data.getMchntTransAmtC()));
        sumData.setDateNotMatchCount(this.getCount(sumData.getDateNotMatchCount())
                + this.getCount(data.getDateNotMatchCount()));
        sumData.setDateNotMatchAmt(this.getAmt(sumData.getDateNotMatchAmt())
                + this.getAmt(data.getDateNotMatchAmt()));
        sumData.setPayMoreCount(this.getCount(sumData.getPayMoreCount())
                + this.getCount(data.getPayMoreCount()));
        sumData.setPayMoreAmt(this.getAmt(sumData.getPayMoreAmt())
                + this.getAmt(data.getPayMoreAmt()));
        sumData.setMchntLaterCount(this.getCount(sumData.getMchntLaterCount())
                + this.getCount(data.getMchntLaterCount()));
        sumData.setMchntLaterAmt(this.getAmt(sumData.getMchntLaterAmt())
                + this.getAmt(data.getMchntLaterAmt()));
        sumData.setPayBeforeCount(this.getCount(sumData.getPayBeforeCount())
                + this.getCount(data.getPayBeforeCount()));
        sumData.setPayBeforeAmt(this.getAmt(sumData.getPayBeforeAmt())
                + this.getAmt(data.getPayBeforeAmt()));
    }

    private void fillSumDataDifferenceAmt(DayBalanceReportData sumData, DayAccountReportData data) {
        sumData.setDifferenceAmt(this.getAmt(sumData.getDifferenceAmt())
                + this.getAmt(data.getDifferenceAmt()));
    }

    private Long getAmt(Long amt) {
        return amt == null ? 0L : amt;
    }

    private Integer getCount(Integer count) {
        return count == null ? 0 : count;
    }

    private void fillMchntTransData(DayBalanceReportMchntDTO mchntDTO, DayBalanceReportData data) {
        data.setMchntTransCountC(mchntDTO.getTransCountC());
        data.setMchntTransAmtC(mchntDTO.getTransAmtC());
        data.setTasterMchnt(mchntDTO.getTaster());
        data.setTasterTimeMchnt(mchntDTO.getTasterTime());
    }

    private void fillBankTransData(DayBalanceReportBankDTO bankDTO, DayBalanceReportData data) {
        data.setBankTransCountD(bankDTO.getTransCountD());
        data.setBankTransAmtD(bankDTO.getTransAmtD());
        data.setBankTransCountC(bankDTO.getTransCountC());
        data.setBankTransAmtC(bankDTO.getTransAmtC());
        data.setTasterBank(bankDTO.getTaster());
        data.setTasterTimeBank(bankDTO.getTasterTime());
    }

    @OperateLogger(content = "生成日结平衡表", operationType = OperateLogger.OperationType.C)
    @RequestMapping("/report/dayBalanceReport/create")
    @RequiresPermissions("dayBalanceReport_create")
    @ResponseBody
    public ResponseDTO<String> create(String settleDate) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            Response<String> response = dayBalanceReportFacade.createDayBalanceReport(
                    DateUtil.parse(settleDate), SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("生成成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("生成失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "支付业务日结平衡表经办", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/busiHandle")
    @RequiresPermissions("dayBalanceReport_busiHandle")
    @ResponseBody
    public ResponseDTO<String> busiHandle(String reportNo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            Response<String> response = dayBalanceReportFacade.busiHandle(
                    reportNo, SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("经办成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("经办失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "支付业务日结平衡表审核通过", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/busiAuditPass")
    @RequiresPermissions("dayBalanceReport_busiAuditPass")
    @ResponseBody
    public ResponseDTO<String> busiAuditPass(String reportNo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            Response<String> response = dayBalanceReportFacade.busiAuditPass(
                    reportNo, SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("审核通过成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("审核通过失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "支付业务日结平衡表审核不通过", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/busiAuditNoPass")
    @RequiresPermissions("dayBalanceReport_busiAuditNoPass")
    @ResponseBody
    public ResponseDTO<String> busiAuditNoPass(String reportNo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            Response<String> response = dayBalanceReportFacade.busiAuditNoPass(
                    reportNo, SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("审核不通过成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("审核不通过失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "备付金日结平衡表经办", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/fundHandle")
    @RequiresPermissions("dayBalanceReport_fundHandle")
    @ResponseBody
    public ResponseDTO<String> fundHandle(String reportNo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            Response<String> response = dayBalanceReportFacade.fundHandle(
                    reportNo, SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("经办成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("经办失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "备付金日结平衡表审核通过", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/fundAuditPass")
    @RequiresPermissions("dayBalanceReport_fundAuditPass")
    @ResponseBody
    public ResponseDTO<String> fundAuditPass(String reportNo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            Response<String> response = dayBalanceReportFacade.fundAuditPass(
                    reportNo, SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("审核通过成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("审核通过失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "备付金日结平衡表审核不通过", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/fundAuditNoPass")
    @RequiresPermissions("dayBalanceReport_fundAuditNoPass")
    @ResponseBody
    public ResponseDTO<String> fundAuditNoPass(String reportNo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            Response<String> response = dayBalanceReportFacade.fundAuditNoPass(
                    reportNo, SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("审核不通过成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("审核不通过失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "日结平衡表审核回退", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/auditRollBack")
    @RequiresPermissions("dayBalanceReport_auditRollBack")
    @ResponseBody
    public ResponseDTO<String> auditRollBack(String reportNo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            Response<String> response = dayBalanceReportFacade.auditRollBack(
                    reportNo, SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("审核回退成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("审核回退失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "重跑备付金数据", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/bankBeforeRebuid")
    @RequiresPermissions("dayBalanceReport_bankBeforeRebuid")
    @ResponseBody
    public ResponseDTO<String> bankBeforeRebuid(String reportNo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            Response<String> response = dayBalanceReportFacade.bankBeforeRebuid(
                    reportNo, SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("重跑备付金数据成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("重跑备付金数据失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "修改T日处理数据", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/bankUpdate")
    @RequiresPermissions("dayBalanceReport_bankUpdate")
    @ResponseBody
    public ResponseDTO<String> bankUpdate(@RequestBody DayAccountReportData dayAccountReportData) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            //组装修改参数
            List<DayBalanceReportBankDTO> dayBalanceReportBankDTOList = new ArrayList<DayBalanceReportBankDTO>();
            boolean bool = false;
            for(DayBalanceReportData dayBalanceReportData : dayAccountReportData.getDayBalanceReportDataList()){
                if(dayBalanceReportData.getPayOrgCode() != null) {
                    DayBalanceReportBankDTO dayBalanceReportBankDTO = new DayBalanceReportBankDTO();
                    dayBalanceReportBankDTO.setReportNo(dayBalanceReportData.getReportNo());
                    dayBalanceReportBankDTO.setSettleDate(dayBalanceReportData.getSettleDate());
                    dayBalanceReportBankDTO.setPayOrgCode(dayBalanceReportData.getPayOrgCode());
                    dayBalanceReportBankDTO.setProvisionsAccountNo(dayBalanceReportData.getProvisionsAccountNo());
                    dayBalanceReportBankDTO.setNoReceiveAmt(dayBalanceReportData.getNoReceiveAmt());
                    dayBalanceReportBankDTO.setBeforeNoReceiveAmt(dayBalanceReportData.getBeforeNoReceiveAmt());
                    dayBalanceReportBankDTO.setBeforeReceiveEarlierAmt(dayBalanceReportData.getBeforeReceiveEarlierAmt());
                    dayBalanceReportBankDTO.setDeleteFlag("0");
                    dayBalanceReportBankDTOList.add(dayBalanceReportBankDTO);
                }else {
                    bool = true;
                }
            }
            DayBalanceReportBankDTO dayBalanceReportBankDTO = new DayBalanceReportBankDTO();
            dayBalanceReportBankDTO.setReportNo(dayAccountReportData.getDayBalanceReportDataList().get(0).getReportNo());
            dayBalanceReportBankDTO.setSettleDate(dayAccountReportData.getDayBalanceReportDataList().get(0).getSettleDate());
            dayBalanceReportBankDTO.setPayOrgCode("0");
            dayBalanceReportBankDTO.setProvisionsAccountNo(dayAccountReportData.getDayBalanceReportDataList().get(0).getProvisionsAccountNo());
            dayBalanceReportBankDTO.setReceiveEarlierAmt(dayAccountReportData.getReceiveEarlierAmt());
            dayBalanceReportBankDTO.setNonBusinessAmt(dayAccountReportData.getNonBusinessAmt());
            if(bool){
                dayBalanceReportBankDTO.setNoReceiveAmt(dayAccountReportData.getDayBalanceReportDataList().get(0).getNoReceiveAmt());
                dayBalanceReportBankDTO.setBeforeNoReceiveAmt(dayAccountReportData.getDayBalanceReportDataList().get(0).getBeforeNoReceiveAmt());
                dayBalanceReportBankDTO.setBeforeReceiveEarlierAmt(dayAccountReportData.getDayBalanceReportDataList().get(0).getBeforeReceiveEarlierAmt());
            }
            dayBalanceReportBankDTO.setDeleteFlag("0");
            dayBalanceReportBankDTOList.add(dayBalanceReportBankDTO);
            Response<String> response = dayBalanceReportFacade.bankUpdate(
                    dayBalanceReportBankDTOList, SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("操作成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("保存失败！", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "支付业务日结平衡表审阅", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/tasterMchnt")
    @RequiresPermissions("dayBalanceReport_tasterMchnt")
    @ResponseBody
    public ResponseDTO<String> tasterMchnt(String reportNo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            DayBalanceReportMchntDTO dayBalanceReportMchntDTO = new DayBalanceReportMchntDTO();
            dayBalanceReportMchntDTO.setTaster(SessionUtils.getUserName());
            dayBalanceReportMchntDTO.setReportNo(reportNo);
            Response<String> response = dayBalanceReportFacade.updateTasterMchnt(dayBalanceReportMchntDTO);
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("审阅成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("审阅失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    @OperateLogger(content = "备付金日结平衡表审阅", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/tasterBank")
    @RequiresPermissions("dayBalanceReport_tasterBank")
    @ResponseBody
    public ResponseDTO<String> tasterBank(String reportNo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            DayBalanceReportBankDTO dayBalanceReportBankDTO = new DayBalanceReportBankDTO();
            dayBalanceReportBankDTO.setTaster(SessionUtils.getUserName());
            dayBalanceReportBankDTO.setReportNo(reportNo);
            Response<String> response = dayBalanceReportFacade.updateTasterBank(dayBalanceReportBankDTO);
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("审阅成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("审阅失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }


    @OperateLogger(content = "修改T日处理数据", operationType = OperateLogger.OperationType.U)
    @RequestMapping("/report/dayBalanceReport/mchntUpdate")
    @RequiresPermissions("dayBalanceReport_mchntUpdate")
    @ResponseBody
    public ResponseDTO<String> mchntUpdate(@RequestBody DayBalanceReportData dayBalanceReportData) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            //组装修改参数
            List<DayBalanceReportMchntDTO> dayBalanceReportMchntDTOList = new ArrayList<DayBalanceReportMchntDTO>();
            if(dayBalanceReportData.getMchntCode() != null) {
                DayBalanceReportMchntDTO dayBalanceReportMchntDTO = new DayBalanceReportMchntDTO();
                dayBalanceReportMchntDTO.setReportNo(dayBalanceReportData.getReportNo());
                dayBalanceReportMchntDTO.setSettleDate(dayBalanceReportData.getSettleDate());
                dayBalanceReportMchntDTO.setMchntCode(dayBalanceReportData.getMchntCode());
                dayBalanceReportMchntDTO.setDeleteFlag("0");
                dayBalanceReportMchntDTO.setDateNotMatchCount(dayBalanceReportData.getDateNotMatchCount());
                dayBalanceReportMchntDTO.setDateNotMatchAmt(dayBalanceReportData.getDateNotMatchAmt());
                dayBalanceReportMchntDTO.setPayMoreCount(dayBalanceReportData.getPayMoreCount());
                dayBalanceReportMchntDTO.setPayMoreAmt(dayBalanceReportData.getPayMoreAmt());
                dayBalanceReportMchntDTO.setMchntLaterCount(dayBalanceReportData.getMchntLaterCount());
                dayBalanceReportMchntDTO.setMchntLaterAmt(dayBalanceReportData.getMchntLaterAmt());
                dayBalanceReportMchntDTO.setPayBeforeCount(dayBalanceReportData.getPayBeforeCount());
                dayBalanceReportMchntDTO.setPayBeforeAmt(dayBalanceReportData.getPayBeforeAmt());
                dayBalanceReportMchntDTOList.add(dayBalanceReportMchntDTO);
            }else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg("商户为空不能调整！");
                return responseDTO;
            }
            Response<String> response = dayBalanceReportFacade.mchntUpdate(
                    dayBalanceReportMchntDTOList, SessionUtils.getUserName());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject("操作成功!!");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(response.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("保存失败！", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }

    /**
     * 亿付数字支付机构、商户侧日结平衡表
     *
     * @param oDate
     * @return
     */
    @RequestMapping("/report/busiDayBalanceReport")
    @ResponseBody
    public ResponseEntity<byte[]> download(String oDate) {
        ResponseEntity<byte[]> responseEntity = null;
        File tempFile = null;
        try {
            InputStream inputStream = new FileInputStream(Constants.getExcelPath() + "/支付机构商户侧日结平衡表.xlsx");
            tempFile = fileDownAjax.touch("支付机构商户侧日结平衡表-" + DateUtils.now() + ".xlsx");
            Pagination<DayBalanceReportData> pagination = new Pagination<>();
            String flag = "0";
            ResponseDTO<List<DayAccountReportData>> responseDTO = search(oDate);
            String stringTime = "";
            String titleTime = "";
            if (responseDTO.getObject().size() > 0) {
                if (responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getCreatedTime() == null) {
                    stringTime = "";
                } else {
                    stringTime = DateUtils.format(responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getCreatedTime(), "yyyyMMdd");
                    titleTime = StringCommonUtils.insertStr(stringTime, "-", 4, 7);
                }
            }


            //表头
            LinkedList<Header> tHeaders = new LinkedList<>();
            //内容
            List<Object> datas = new ArrayList<>();
            //添加表头
            tHeaders.add(new HeaderExt("机构清算日期", "settleDate"));
            tHeaders.add(new HeaderExt("支付机构名称", "payOrgCode", ExcelContentExt.ORGANIZE));
            tHeaders.add(new HeaderExt("笔数", "count1"));
            tHeaders.add(new HeaderExt("金额（应收款项）", "amt1", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("商户名称", "mchntCode", ExcelContentExt.MCHNT));
            tHeaders.add(new HeaderExt("笔数", "count2"));
            tHeaders.add(new HeaderExt("金额", "amt2", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("总笔数", "count3"));
            tHeaders.add(new HeaderExt("总金额", "amt3", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("笔数", "dateNotMatchCount"));
            tHeaders.add(new HeaderExt("金额", "dateNotMatchAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("笔数", "mchntLaterCount"));
            tHeaders.add(new HeaderExt("金额", "mchntLaterAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("笔数", "payMoreCount"));
            tHeaders.add(new HeaderExt("金额", "payMoreAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("笔数", "payBeforeCount"));
            tHeaders.add(new HeaderExt("金额", "payBeforeAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("笔数", "correspondOrgCountD"));
            tHeaders.add(new HeaderExt("金额", "correspondOrgAmtD", ExcelContentExt.CURRENCY));
            for (DayAccountReportData dayAccountReportData : responseDTO.getObject()) {
                for (DayBalanceReportData dayBalanceReportData : dayAccountReportData.getDayBalanceReportDataList()) {
                    if(dayBalanceReportData.getPayOrgCode() != null || dayBalanceReportData.getMchntCode() != null) {
                        Map map = Bean2MapUtil.convertBean(dayBalanceReportData);
                        map.put("count1", dayBalanceReportData.getBankTransCountD() + dayBalanceReportData.getBankTransCountC());
                        map.put("amt1", dayBalanceReportData.getBankTransAmtD() - dayBalanceReportData.getBankTransAmtC());
                        map.put("count2", dayBalanceReportData.getCorrespondOrgCountC() + dayBalanceReportData.getCorrespondOrgCountD());
                        map.put("amt2", dayBalanceReportData.getCorrespondOrgAmtC() - dayBalanceReportData.getCorrespondOrgAmtD());
                        map.put("count3", dayBalanceReportData.getMchntTransCountC());
                        map.put("amt3", dayBalanceReportData.getMchntTransAmtC());
                        datas.add(map);
                    }
                }
            }
            /**
             * 增加一行合计 edit by francis.hu
             */
            if (responseDTO.getObject().size() > 0) {
                DayBalanceReportData dayBalanceReportData = (DayBalanceReportData) responseDTO.getSumObject();
                Map map = Bean2MapUtil.convertBean(DayBalanceReportData.class);
                map.put("payOrgCode", "合计");
                map.put("count1", dayBalanceReportData.getBankTransCountD() + dayBalanceReportData.getBankTransCountC());
                map.put("amt1", dayBalanceReportData.getBankTransAmtD() - dayBalanceReportData.getBankTransAmtC());
                map.put("mchntCode", "合计");
                map.put("count2", dayBalanceReportData.getCorrespondOrgCountC() + dayBalanceReportData.getCorrespondOrgCountD());
                map.put("amt2", dayBalanceReportData.getCorrespondOrgAmtC() - dayBalanceReportData.getCorrespondOrgAmtD());
                map.put("count3", dayBalanceReportData.getMchntTransCountC());
                map.put("amt3", dayBalanceReportData.getMchntTransAmtC());
                map.put("dateNotMatchCount", dayBalanceReportData.getDateNotMatchCount());
                map.put("dateNotMatchAmt", dayBalanceReportData.getDateNotMatchAmt());
                map.put("mchntLaterCount", dayBalanceReportData.getMchntLaterCount());
                map.put("mchntLaterAmt", dayBalanceReportData.getMchntLaterAmt());
                map.put("payMoreCount", dayBalanceReportData.getPayMoreCount());
                map.put("payMoreAmt", dayBalanceReportData.getPayMoreAmt());
                map.put("payBeforeCount", dayBalanceReportData.getPayBeforeCount());
                map.put("payBeforeAmt", dayBalanceReportData.getPayBeforeAmt());
                map.put("correspondOrgCountD", dayBalanceReportData.getCorrespondOrgCountD());
                map.put("correspondOrgAmtD", dayBalanceReportData.getCorrespondOrgAmtD());
                datas.add(map);
            }

            Map map1 = Bean2MapUtil.convertBean(DayBalanceReportData.class);
            if (responseDTO.getObject().size() > 0) {
                if (StringUtils.isEmpty(responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getBusiHandler())) {
                    map1.put("settleDate", "经办员：");
                } else {
                    map1.put("settleDate", "经办员：" + responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getBusiHandler());
                }
                if (StringUtils.isEmpty(responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getBusiAuditor())) {
                    map1.put("count3", "复核员：");
                } else {
                    map1.put("count3", "复核员：" + responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getBusiAuditor());
                }
                if(StringUtils.isEmpty(responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getTasterMchnt())){
                    map1.put("payMoreCount","主管：");
                }else {
                    map1.put("payMoreCount","主管：" + responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getTasterMchnt());
                }
            } else {
                map1.put("settleDate", "经办员：");
                map1.put("count3", "复核员：");
                map1.put("payMoreCount","主管：");

            }
            datas.add(map1);
            ExcelWriterConfig config = new ExcelWriterConfig();
            config.setHeaders(tHeaders);
            config.setDatas(datas);
            config.setStartWith(6);
            FileOutputStream outputStream = new FileOutputStream(tempFile);
            ExcelTemplateUtil.write(inputStream, outputStream, config, titleTime, flag,null);
            responseEntity = fileDownAjax.getResponseEntity(tempFile);
        } catch (Exception e) {
            log.error("下载失败", e);
            responseEntity = fileDownAjax.getResponseEntityFail();
        } finally {
            fileDownAjax.forceDelete(tempFile);
        }
        return responseEntity;
    }

    /**
     * 支付业务备付金日结平衡表
     *
     * @param oDate
     * @return
     */
    @RequestMapping("/report/fundDayBalanceReport")
    @ResponseBody
    public ResponseEntity<byte[]> downloadFundDayBalanceReport(String oDate) {
        ResponseEntity<byte[]> responseEntity = null;
        File tempFile = null;
        try {
            InputStream inputStream = new FileInputStream(Constants.getExcelPath() + "/支付业务备付金日结平衡表.xlsx");
            tempFile = fileDownAjax.touch("支付业务备付金日结平衡表-" + DateUtils.now() + ".xlsx");
            Pagination<DayBalanceReportData> pagination = new Pagination<>();
            String titleTime = StringCommonUtils.insertStr(oDate, "-", 4, 7);
            String flag = "1";
            ResponseDTO<List<DayAccountReportData>> responseDTO = search(oDate);
            //表头
            LinkedList<Header> tHeaders = new LinkedList<>();
            //内容
            List<Object> datas = new ArrayList<>();
            //添加表头
            tHeaders.add(new HeaderExt("机构清算日期", "settleDate"));
            tHeaders.add(new HeaderExt("支付机构名称", "payOrgCode", ExcelContentExt.ORGANIZE));
            tHeaders.add(new HeaderExt("笔数", "count1"));
            tHeaders.add(new HeaderExt("金额（应收款项）", "amt1", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("银行账户名称", "provisionsAccountName", ExcelContentExt.BANK_INFO));
            tHeaders.add(new HeaderExt("T日发生额", "provisionsAccrualAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("差异", "differenceAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("T日支付，T日未达金额（1）", "noReceiveAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("T-N日未达,T日资金已达（2）", "beforeNoReceiveAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("T日先达金额(3)", "receiveEarlierAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("T-N日资金先达，T日数据已达(4)", "beforeReceiveEarlierAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("非业务金额（5）", "nonBusinessAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("商户名称", "mchntCode", ExcelContentExt.MCHNT));
            tHeaders.add(new HeaderExt("T日销账成功（1）", "amt2", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("其中：T-N日支付，T日销账成功（2）", "dateNotMatchAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("T+N日支付，T日销账A", "mchntLaterAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("T日待销账（3）", "payMoreAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("T日支付，T-N日已销账B", "payBeforeAmt", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("T日前支付未销，在T日完成退款（4）", "correspondOrgAmtD", ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("合计", "sumAmt", ExcelContentExt.CURRENCY));
            for (DayAccountReportData dayAccountReportData : responseDTO.getObject()) {
                for (DayBalanceReportData dayBalanceReportData : dayAccountReportData.getDayBalanceReportDataList()) {
                    dayBalanceReportData.setProvisionsAccountName(dayAccountReportData.getProvisionsAccountName());
                    dayBalanceReportData.setProvisionsAccrualAmt(dayAccountReportData.getProvisionsAccrualAmt());
                    dayBalanceReportData.setDifferenceAmt(dayAccountReportData.getDifferenceAmt());
                    dayBalanceReportData.setReceiveEarlierAmt(dayAccountReportData.getReceiveEarlierAmt());
                    dayBalanceReportData.setNonBusinessAmt(dayAccountReportData.getNonBusinessAmt());
                    Map map = Bean2MapUtil.convertBean(dayBalanceReportData);
                    map.put("count1", dayBalanceReportData.getBankTransCountD() + dayBalanceReportData.getBankTransCountC());
                    map.put("amt1", dayBalanceReportData.getBankTransAmtD() - dayBalanceReportData.getBankTransAmtC());
                    map.put("amt2", dayBalanceReportData.getMchntTransAmtC());
                    map.put("sumAmt",dayBalanceReportData.getMchntTransAmtC() -
                            dayBalanceReportData.getDateNotMatchAmt() + dayBalanceReportData.getPayMoreAmt() - dayBalanceReportData.getCorrespondOrgAmtD()
                    - dayBalanceReportData.getMchntLaterAmt() + dayBalanceReportData.getPayBeforeAmt());
                    datas.add(map);
                }
            }
            /**
             * 增加一行合计 edit by francis.hu
             */
            if (responseDTO.getObject().size() > 0) {
                DayBalanceReportData dayBalanceReportData = (DayBalanceReportData) responseDTO.getSumObject();
                Map map = Bean2MapUtil.convertBean(DayBalanceReportData.class);
                map.put("payOrgCode", "合计");
                map.put("count1", dayBalanceReportData.getBankTransCountD() + dayBalanceReportData.getBankTransCountC());
                map.put("amt1", dayBalanceReportData.getBankTransAmtD() - dayBalanceReportData.getBankTransAmtC());
                map.put("provisionsAccountName","合计");
                map.put("provisionsAccrualAmt",dayBalanceReportData.getProvisionsAccrualAmt());
                map.put("differenceAmt",dayBalanceReportData.getDifferenceAmt());
                map.put("noReceiveAmt",dayBalanceReportData.getNoReceiveAmt());
                map.put("beforeNoReceiveAmt",dayBalanceReportData.getBeforeNoReceiveAmt());
                map.put("receiveEarlierAmt",dayBalanceReportData.getReceiveEarlierAmt());
                map.put("beforeReceiveEarlierAmt",dayBalanceReportData.getBeforeReceiveEarlierAmt());
                map.put("nonBusinessAmt",dayBalanceReportData.getNonBusinessAmt());
                map.put("mchntCode","合计");
                map.put("amt2", dayBalanceReportData.getMchntTransAmtC());
                map.put("dateNotMatchAmt",dayBalanceReportData.getDateNotMatchAmt());
                map.put("mchntLaterAmt",dayBalanceReportData.getMchntLaterAmt());
                map.put("payMoreAmt",dayBalanceReportData.getPayMoreAmt());
                map.put("payBeforeAmt",dayBalanceReportData.getPayBeforeAmt());
                map.put("correspondOrgAmtD",dayBalanceReportData.getCorrespondOrgAmtD());
                map.put("sumAmt",dayBalanceReportData.getMchntTransAmtC() -
                        dayBalanceReportData.getDateNotMatchAmt() + dayBalanceReportData.getPayMoreAmt() - dayBalanceReportData.getCorrespondOrgAmtD()
                -dayBalanceReportData.getMchntLaterAmt() + dayBalanceReportData.getPayBeforeAmt());
                datas.add(map);
            }

            Map map1 = Bean2MapUtil.convertBean(DayBalanceReportData.class);
            if (responseDTO.getObject().size() > 0) {
                if (StringUtils.isEmpty(responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getFundHandler())) {
                    map1.put("settleDate", "经办员：");
                } else {
                    map1.put("settleDate", "经办员：" + responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getFundHandler());
                }
                if (StringUtils.isEmpty(responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getFundAuditor())) {
                    map1.put("provisionsAccountName", "复核员：");
                } else {
                    map1.put("provisionsAccountName", "复核员：" + responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getFundAuditor());
                }
                if(StringUtils.isEmpty(responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getTasterBank())){
                    map1.put("mchntCode","主管：");
                }else {
                    map1.put("mchntCode","主管：" + responseDTO.getObject().get(0).getDayBalanceReportDataList().get(0).getTasterBank());
                }
            } else {
                map1.put("settleDate", "经办员：");
                map1.put("provisionsAccountName", "复核员：");
                map1.put("mchntCode","主管：");
            }
            datas.add(map1);
            ExcelWriterConfig config = new ExcelWriterConfig();
            config.setHeaders(tHeaders);
            config.setDatas(datas);
            config.setStartWith(6);
            FileOutputStream outputStream = new FileOutputStream(tempFile);
            ExcelTemplateUtil.write(inputStream, outputStream, config, titleTime, flag,responseDTO.getObject());
            responseEntity = fileDownAjax.getResponseEntity(tempFile);
        } catch (Exception e) {
            log.error("下载失败", e);
            responseEntity = fileDownAjax.getResponseEntityFail();
        } finally {
            fileDownAjax.forceDelete(tempFile);

        }
        return responseEntity;
    }

    @RequestMapping("/report/dayBalanceReport/busiPrint")
    @ResponseBody
    @OperateLogger(content = "亿付数字支付机构，商户侧日结平衡表打印",operationType = OperateLogger.OperationType.R)
    public Map busiPrint(){
        log.info("亿付数字支付机构，商户侧日结平衡表打印---");

        return new HashMap();
    }

    @RequestMapping("/report/dayBalanceReport/fundPrint")
    @ResponseBody
    @OperateLogger(content = "支付业务备付金日结平衡表打印",operationType = OperateLogger.OperationType.R)
    public Map fundPrint(){
        log.info("支付业务备付金日结平衡表打印---");
        return new HashMap();
    }
}
